/*
 * @file guicb.c GUI callback functions.
 *
 * @Copyright (C) 2007 Kris Marsh.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "guicb.h"

#define GUI_REFRESH_RATE 50

static guint update_timer = 0;

void
guicb_main_destroy (GtkObject *object,
		    gpointer user_data)
{
	g_debug("Destroying");
	gtk_main_quit();
	gst_destroy_pipeline();
}

/* Print the current play position to the terminal */
static gboolean
cb_print_position ()
{
	gui_update_display();

	/* call me again */
	return TRUE;
}


void
guicb_playbutton (GtkWidget *widget,
                  gpointer data)
{
	gboolean toggled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
	if (toggled == TRUE) {
		g_debug("Playing");

		/* Set a timer, to update the current playback position. */
		g_assert(update_timer == 0);
		update_timer = g_timeout_add(GUI_REFRESH_RATE, (GSourceFunc)cb_print_position, NULL);

		gst_play(); /* This is a blocking function. */
	} else {
		/* Cancel the update timer. */
		g_source_remove(update_timer);
		update_timer = 0;

		g_debug("Paused");
		gst_pause();

		/* One-shot update the GUI, to show paused status. */
		gui_update_display();
	}
}


void
guicb_rewindbutton (GtkWidget *widget,
                  gpointer data)
{
	g_debug("Rewinding");
	gst_rewind();

	/* One-shot update the GUI. */
	gui_update_display();
}

void
guicb_seek_button (GtkWidget *widget,
                  gpointer data)
{
	int amnt = GPOINTER_TO_INT(data);

	g_debug("Seeking %i", amnt);
	gst_seek(amnt);

	/* One-shot update the GUI. */
	gui_update_display();
}

void
guicb_pitchchange(GtkWidget *widget,
		  gpointer data)
{
	/* Get current pitch/scale value and set the new pitch. */
	gdouble new_pitch = gtk_range_get_value(GTK_RANGE(widget));
	gst_change_pitch(new_pitch);
}


void guicb_dnd(GtkWidget *widget, GdkDragContext *dc, gint x, gint y, GtkSelectionData *selection_data, guint info, guint t, gpointer data)
{
	gchar **uri_list = NULL;
	gchar *filename = NULL;
	gint retval = FALSE;

	if (!selection_data)
		return;

        if (selection_data->length < 0)
                return;

	g_debug("Dnd event received. Data: '%s'", selection_data->data);

	/* Might be a basic string, or a list of URIs. */
	switch (info) {
	case TARGET_URI:
		g_debug("Drag type: URI list");

		/* Get an array of URIs from the selection. */
		uri_list = gtk_selection_data_get_uris(selection_data);

		if (!uri_list)
			return;

		filename = g_strdup(uri_list[0]);
		g_strfreev(uri_list);
	
		break;
	case TARGET_PLAIN:
		g_debug("Drag type: plain");
		filename = gtk_selection_data_get_text(selection_data);
		break;
	case TARGET_STRING:
		g_debug("Drag type: string");
		filename = gtk_selection_data_get_text(selection_data);
		break;
	default:
		g_print("Unknown object dropped");
		return;
		break;
	}

	g_debug("Filename: %s\n", filename);

	/* Load the URI. */
	retval = gst_load_uri(filename);

	if (retval == FALSE) {
		g_print("Error loading URI.\n");
	}

	gui_enable_controls(TRUE);
}

